package otcv.hr.util;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author : H-Thien
 * @comment : Date Uril
 */
public class DateUtil {

	private String year;
	private String month;
	private String day;
	private String hour;
	private String min;
	private String sec;

	/**
	 * @comment :
	 */
	public DateUtil() {
		try {
			Calendar calendar = getCurTimeStamp();
			year = String.valueOf(calendar.get(Calendar.YEAR));
			month = String.valueOf((calendar.get(Calendar.MONTH) + 1));
			day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
			hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
			min = String.valueOf(calendar.get(Calendar.MINUTE));
			sec = String.valueOf(calendar.get(Calendar.SECOND));
		} catch (Exception e) {

		}
	}

	/**
	 * @comment :
	 */
	public DateUtil(String s) {
		try {
			year = s.substring(0, 4);
			month = s.substring(4, 6);
			day = s.substring(6, 8);
			hour = s.substring(8, 10);
			min = s.substring(10, 12);
			sec = s.substring(12, 14);
		} catch (Exception e) {

		}
	}

	/**
	 * @comment :
	 */
	public static Calendar getCurTimeStamp() {
		Locale lc = new Locale("Locale.KOREAN", "Locale.KOREA");
		TimeZone mySTZ = TimeZone.getTimeZone("JST");
		Calendar calendar = Calendar.getInstance(mySTZ, lc);

		return calendar;
	}

	/**
	 * @comment : 1 : YYYYMMDD 2 : YYYY年 MM月 DD日 3 : YYYY.MM.DD 4 :
	 *          YYYYMMDDHHMISS 5 : YYYY/MM/DD 6 : YYYYMM 7 : YYYY/MM/DD HH:MI:SS
	 *          8 : YYYY 9 : MM
	 */
	public static String getCurrentDateInfo(String part) {
		String returnDate = "";

		Calendar calendar = getCurTimeStamp();
		int year = calendar.get(Calendar.YEAR);
		int month = (calendar.get(Calendar.MONTH) + 1);
		int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
		int hh = calendar.get(Calendar.HOUR_OF_DAY);
		int mi = calendar.get(Calendar.MINUTE);
		int ss = calendar.get(Calendar.SECOND);

		if (part.equals("1")) {
			returnDate += year;
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
			if (day_of_month < 10) {
				returnDate += "0";
			}
			returnDate += day_of_month;
		} else if (part.equals("2")) {
			returnDate += year + "年 ";
			returnDate += month + "月 ";
			returnDate += day_of_month + "日";
		} else if (part.equals("3")) {
			returnDate += year + ".";
			returnDate += month + ".";
			returnDate += day_of_month;
		} else if (part.equals("4")) {
			returnDate += year;

			if (month < 10)
				returnDate += "0";
			returnDate += month;

			if (day_of_month < 10)
				returnDate += "0";
			returnDate += day_of_month;

			if (hh < 10)
				returnDate += "0";
			returnDate += hh;

			if (mi < 10)
				returnDate += "0";
			returnDate += mi;

			if (ss < 10)
				returnDate += "0";
			returnDate += ss;
		} else if (part.equals("5")) {
			returnDate += year + "/";

			if (month < 10)
				returnDate += "0";
			returnDate += month + "/";

			if (day_of_month < 10)
				returnDate += "0";
			returnDate += day_of_month;
		} else if (part.equals("6")) {
			returnDate += year;
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
		} else if (part.equals("7")) {
			returnDate += year + "/";

			if (month < 10)
				returnDate += "0";
			returnDate += month + "/";

			if (day_of_month < 10)
				returnDate += "0";
			returnDate += day_of_month + " ";

			if (hh < 10)
				returnDate += "0";
			returnDate += hh + ":";

			if (mi < 10)
				returnDate += "0";
			returnDate += mi + ":";

			if (ss < 10)
				returnDate += "0";
			returnDate += ss;
		} else if (part.equals("8")) {
			returnDate += year;
		} else if (part.equals("9")) {
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
		} else {
		}

		return returnDate;
	}

	/**
	 * @comment : 1 : YYYYMMDD 2 : YYYY年 MM月 DD日 3 : YYYY.MM.DD 4 :
	 *          YYYYMMDDHHMISS 5 : YYYY/MM/DD 6 : YYYYMM 7 : YYYY-MM-DD 8 : YYYY
	 *          9 : MM
	 **/
	public static String getCurrentDateInfo(String part, String field, int amount) {
		String returnDate = "";

		Calendar calendar = getCurTimeStamp();
		if (field.equals("year")) {
			calendar.add(Calendar.YEAR, amount);
		} else if (field.equals("month")) {
			calendar.add(Calendar.MONTH, amount);
		} else if (field.equals("day")) {
			calendar.add(Calendar.DAY_OF_MONTH, amount);
		} else if (field.equals("hour")) {
			calendar.add(Calendar.HOUR_OF_DAY, amount);
		} else if (field.equals("minute")) {
			calendar.add(Calendar.MINUTE, amount);
		} else if (field.equals("second")) {
			calendar.add(Calendar.SECOND, amount);
		} else {

		}

		int year = calendar.get(Calendar.YEAR);
		int month = (calendar.get(Calendar.MONTH) + 1);
		int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
		int hh = calendar.get(Calendar.HOUR_OF_DAY);
		int mi = calendar.get(Calendar.MINUTE);
		int ss = calendar.get(Calendar.SECOND);

		if (part.equals("1")) {
			returnDate += year;
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
			if (day_of_month < 10) {
				returnDate += "0";
			}
			returnDate += day_of_month;
		} else if (part.equals("6")) {
			returnDate += year;
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
		} else if (part.equals("2")) {
			returnDate += year + "年 ";
			returnDate += month + "月 ";
			returnDate += day_of_month + "日";
		} else if (part.equals("3")) {
			returnDate += year + ". ";
			returnDate += month + ". ";
			returnDate += day_of_month;
		} else if (part.equals("4")) {
			returnDate += year;

			if (month < 10)
				returnDate += "0";
			returnDate += month;

			if (day_of_month < 10)
				returnDate += "0";
			returnDate += day_of_month;

			if (hh < 10)
				returnDate += "0";
			returnDate += hh;

			if (mi < 10)
				returnDate += "0";
			returnDate += mi;

			if (ss < 10)
				returnDate += "0";
			returnDate += ss;
		} else if (part.equals("5")) {
			returnDate += year + "/";

			if (month < 10)
				returnDate += "0";
			returnDate += month + "/";

			if (day_of_month < 10)
				returnDate += "0";
			returnDate += day_of_month;
		} else if (part.equals("6")) {
			returnDate += year;
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
		} else if (part.equals("7")) {
			returnDate += year + "-";

			if (month < 10)
				returnDate += "0";
			returnDate += month + "-";

			if (day_of_month < 10)
				returnDate += "0";
			returnDate += day_of_month;
		} else if (part.equals("8")) {
			returnDate += year;
		} else if (part.equals("9")) {
			if (month < 10) {
				returnDate += "0";
			}
			returnDate += month;
		} else {
		}

		return returnDate;
	}

	/**
	 * @comment :
	 */
	public static String getDateTimeFormat(String s) {
		String dt = "";
		try {
			dt += s.substring(0, 4) + "年 ";
			dt += s.substring(4, 6) + "月 ";
			dt += s.substring(6, 8) + "日 ";
			int hour = Integer.parseInt(s.substring(8, 10));
			dt += hour > 13 ? " 午後 " : " 午前 ";
			hour -= hour > 13 ? 12 : 0;
			dt += (hour < 10 ? "0" + hour : Integer.toString(hour)) + "時 ";
			dt += s.substring(10, 12) + "分 ";
		} catch (Exception e) {
			return "";
		}

		return dt;
	}

	/**
	 * @comment :
	 */
	public static String getDateTimeFormatHour(String s) {
		String dt = "";
		try {
			dt += s.substring(0, 4) + ".";
			dt += s.substring(4, 6) + ".";
			dt += s.substring(6, 8) + " ";
			dt += s.substring(8, 10) + ":";
			dt += s.substring(10, 12) + ":";
			dt += s.substring(12, 14);
		} catch (Exception e) {
			return s;
		}

		return dt;
	}

	/**
	 * @comment :
	 */
	public static String getDateTimeFormatHyphen(String s) {
		String dt = "";
		try {
			dt += s.substring(0, 4) + "-";
			dt += s.substring(4, 6) + "-";
			dt += s.substring(6, 8);
		} catch (Exception e) {
			return s;
		}

		return dt;
	}

	/**
	 * @comment :
	 */
	public static String getDateTimeFormatEng(String s) {
		String dt = "";
		String[] month = { "X", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
		try {
			dt += month[Integer.parseInt(s.substring(4, 6), 0)];
			dt += "-" + s.substring(6, 8);
			dt += "-" + s.substring(0, 4);
		} catch (Exception e) {
			return "";
		}

		return dt;
	}

	/**
	 * @comment :
	 */
	public static String getSelectBoxYear(int sYear, String chkYear) {
		Calendar calendar = getCurTimeStamp();
		int pYear = calendar.get(Calendar.YEAR);

		if (Util.isNull(chkYear)) {
			chkYear = String.valueOf(pYear);
		}

		StringBuffer str = new StringBuffer();
		for (int i = pYear; i >= sYear; i--) {
			if (String.valueOf(i).equals(chkYear)) {
				str.append("<option value='" + i + "' selected>" + i + "</option>");
			} else {
				str.append("<option value='" + i + "'>" + i + "</option>");
			}
		}

		return str.toString();
	}

	/**
	 * @comment :
	 */
	public static String getSelectBoxMonth(String chkMonth, String type) {
		Calendar calendar = getCurTimeStamp();
		int pMonth = (calendar.get(Calendar.MONTH) + 1);

		if (Util.isNull(chkMonth)) {
			chkMonth = String.valueOf(pMonth);
		}

		int maxMonth = 0;
		if (type.equals("A")) {
			maxMonth = pMonth;
		} else {
			maxMonth = 12;
		}

		StringBuffer str = new StringBuffer();
		String value = "";
		for (int i = maxMonth; i >= 1; i--) {
			if (i < 10) {
				value = "0" + i;
			} else {
				value = String.valueOf(i);
			}
			if (String.valueOf(i).equals(chkMonth)) {
				str.append("<option value='" + value + "' selected>" + i + "</option>");
			} else {
				str.append("<option value='" + value + "'>" + i + "</option>");
			}
		}

		return str.toString();
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public String getHour() {
		return hour;
	}

	public void setHour(String hour) {
		this.hour = hour;
	}

	public String getMin() {
		return min;
	}

	public void setMin(String min) {
		this.min = min;
	}

	public String getSec() {
		return sec;
	}

	public void setSec(String sec) {
		this.sec = sec;
	}
}